/**
 * 参考链接: https://github.com/vitejs/vite/blob/master/src/node/config.ts
 */
const { join, resolve } = require('path')
import vuePlugin from 'rollup-plugin-vue'
import vue from '@vitejs/plugin-vue'
// import { UserConfig } from 'vite'
import dotenv from 'dotenv'
const commonjs = require('@rollup/plugin-commonjs');

dotenv.config({ path: join(__dirname, '.env') })
const root = join(__dirname, 'src/render')
const config = {
  server: {

    port: +process.env.PORT,
  },
  // base: '../',
  outDir: resolve(__dirname, 'dist/render'),
  plugins: [
    vue(),
    // commonjs()
  ],
  build: {
    // polyfillDynamicImport:false,
    emptyOutDir: true,
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'src/render/windows/pages/Provider/view/index.html'),
        browserWin: resolve(__dirname, 'src/render/windows/pages/BrowserWindow/view/index.html'),
      },
      external: [
        'crypto',
        'assert',
        'fs',
        'util',
        'os',
        'events',
        'child_process',
        'http',
        'https',
        'path',
        'electron',
      ],
      output: {
        dir: './dist/render',
        format: 'commonjs',
      },
      plugins: [
        // {
        //   name: '@rollup/plugin-cjs2esm',
        //   transform(code, filename) {
        //     if (filename.includes('/node_modules/')) {
        //       return code
        //     }

        //     const cjsRegexp = /(const|let|var)[\n\s]+(\w+)[\n\s]*=[\n\s]*require\(["|'](.+)["|']\)/g
        //     const res = code.match(cjsRegexp)
        //     if (res) {
        //       // const Store = require('electron-store') -> import Store from 'electron-store'
        //       code = code.replace(cjsRegexp, `import $2 from '$3'`)
        //     }
        //     return code
        //   },
        // }
      ],
    }
  },
  alias: {
    // 别名必须以 / 开头、结尾
    // '/@/': root, -- vite 内部在用，这里不能用了
    // '/root/': __dirname, -- vite 内部在用，这里不能用了
    // '/assets/': join(__dirname, 'src/render/assets'),
    // '/components/': join(__dirname, 'src/render/components'),
    // '/lib/': join(__dirname, 'src/render/lib'),
    // '/utils/': join(__dirname, 'src/render/utils'),
    // '/views/': join(__dirname, 'src/render/views'),
  },
  optimizeDeps: {
    // 这里不加也没事，用 require 的形式就能避开 import 被编译成 /@modules/fs?import
    // allowNodeBuiltins: ['electron-is-dev', 'electron-store', 'electron']
  }
}

export default config